
import Lexer.TokenTable;
import Lexer.Lexer;
import Syntax_Anlayzer.*;

import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;

import java.io.*;

public class Main {
    static int cnt;
    public static void output() {
        Iterator<String> it = First.first.keySet().iterator();
        cnt = 1;
        System.out.println("first:");
        while (it.hasNext()) {
            Object key = it.next();
            Object value = First.first.get(key);
            System.out.println(cnt++ + " " + key + " " + value);
        }
        System.out.println();

        cnt = 1;
        System.out.println("V_N:");
        for (String str : Init.V_N) {
            System.out.println(cnt++ + " " + str);
        }
        System.out.println();

        cnt = 1;
        System.out.println("V_T:");
        for (String str : Init.V_T) {
            System.out.println(cnt++ + " " + str);
        }
        System.out.println();
        Iterator<String> it2 = Init.Production.keySet().iterator();
        cnt = 1;
        System.out.println("Production:");
        while (it2.hasNext()) {
            Object key = it2.next();
            Object value = Init.Production.get(key);
            System.out.println(cnt++ + " " + key + " " + value);
        }
        System.out.println();

        Iterator<List<String>> it3 = Follow.firstX.keySet().iterator();
        cnt = 1;
        System.out.println("firstX:");
        while (it3.hasNext()) {
            Object key = it3.next();
            Object value = Follow.firstX.get(key);
            System.out.println(cnt++ + " " + key + " " + value);
        }
        System.out.println();

        Iterator<String> it4 = Follow.follow.keySet().iterator();
        cnt = 1;
        System.out.println("follow:");
        while (it4.hasNext()) {
            Object key = it4.next();
            Object value = Follow.follow.get(key);
            System.out.println(cnt++ + " " + key + " " + value);
        }
        System.out.println();


//        System.out.println("预测分析表:");
//        ForecastAnalysisTable.displayTable(); // 调用PredictiveTable类的displayTable方法来显示预测分析表
//        System.out.println();

    }
    //测试样例
    public static void main(String[] args) {
        String filePath = "D:\\byyl\\c--compiler\\Syntax_Anlayzer\\Examples\\02.sy"; // 文件路径
        List<String> Tokens = new ArrayList<>();
        String content1= new String();
        try {
            // 使用Files.readString方法读取文件内容
            List<String> lines = Files.readAllLines(Paths.get(filePath), StandardCharsets.UTF_8);
            String content = String.join(System.lineSeparator(), lines);
            content1=content;

            LexicalAnalyzer lexer = new LexicalAnalyzer();
            List<Token1> tokens = lexer.analyze(content);

            for (Token1 token : tokens) {
                Tokens.add(token.toString());
            }

        } catch (IOException e) {
            // 异常处理：文件不存在、没有读取权限等
            e.printStackTrace();
        }
        
        for (String token : Tokens) {
            System.out.println(token);
        }
        
       
        System.out.println("");
        First f = new First(Init.getInstance("D:\\byyl\\c--compiler\\Syntax_Anlayzer\\src\\grammar.txt"));
        f.constructFirst();
        f.constructFollow();
        output();
        ForecastAnalysisTable forecastAnalysisTable=new ForecastAnalysisTable();
        forecastAnalysisTable.buildTable();
        System.out.println();
        Iterator iterator1=ForecastAnalysisTable.forecastAnalysisTable.keySet().iterator();
        System.out.println("ForecastAnalysisTable:");
        while (iterator1.hasNext()){
            String V_N=(String) iterator1.next();
            Map map=ForecastAnalysisTable.forecastAnalysisTable.get(V_N);
            Iterator iterator2=map.keySet().iterator();
            while (iterator2.hasNext()){
                String V_T=(String) iterator2.next();
                List list=(List) map.get(V_T);
                System.out.println(V_N+" "+V_T+" "+list);
            }
        }
        String startSymbol = "program";
        //TokenTable tokenTable = new TokenTable();
        
        ForecastAnalysis forecastAnalysis=new ForecastAnalysis(content1,startSymbol );
        System.out.println("ForecastAnalysis:");
        forecastAnalysis.forecastAnalysis();
    }
}